package cn.com.greatwall.kafka.rest;

import java.io.IOException;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.common.exception.BadRequestException;
import cn.com.greatwall.kafka.domain.Cluster;
import cn.com.greatwall.kafka.service.ClusterService;
import cn.com.greatwall.kafka.service.KafkaService;
import cn.com.greatwall.kafka.service.ZooKeeperService;
import cn.com.greatwall.kafka.service.dto.criteria.ClusterQueryCriteria;
import cn.com.greatwall.logging.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2021-01-12 16:38:30
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "MQ：Kafka集群管理")
@RequestMapping("/api/mq/cluster")
public class ClusterController {
    private final ClusterService clusterService;
    private final ZooKeeperService zooKeeperService;
    private final KafkaService kafkaService;
    private static final String ENTITY_NAME = "cluster";
    
    @Log("导出集群")
    @ApiOperation("导出集群")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('cluster:list')")
    public void download(HttpServletResponse response, ClusterQueryCriteria criteria) throws IOException{
        clusterService.download(clusterService.queryAll(criteria), response);
    }

    @Log("查询集群")
    @ApiOperation("查询集群")
    @GetMapping
    @PreAuthorize("@el.check('cluster:list')")
    public ResponseEntity<Object> query(ClusterQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(clusterService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("查询集群")
    @ApiOperation("返回全部的集群")
    @GetMapping(value = "/all")
    @PreAuthorize("@el.check('cluster:list','cluster:add','cluster:edit')")
    public ResponseEntity<Object> query() {
        return new ResponseEntity<>(clusterService.queryAll(), HttpStatus.OK);
    }
    
    @Log("新增集群")
    @ApiOperation("新增集群")
    @PostMapping
    @PreAuthorize("@el.check('cluster:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Cluster resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        clusterService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    
    @Log("修改集群")
    @ApiOperation("修改集群")
    @PutMapping
    @PreAuthorize("@el.check('cluster:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Cluster resources) {
        clusterService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除集群")
    @ApiOperation("删除集群")
    @DeleteMapping
    @PreAuthorize("@el.check('cluster:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
        clusterService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("校验zookeeper")
    @ApiOperation("校验zookeeper")
    @PostMapping("/validate/zooKeeper")
    @PreAuthorize("@el.check('cluster:list')")
    public ResponseEntity<Object> testConnectZookeeper(@Validated @RequestBody Cluster resources) {
        return new ResponseEntity<>(zooKeeperService.testConnectZookeeper(resources), HttpStatus.CREATED);
    }

    @Log("校验kafka")
    @ApiOperation("校验kafka")
    @PostMapping("/validate/kafka")
    @PreAuthorize("@el.check('cluster:list')")
    public ResponseEntity<Object> testConnectKafka(@Validated @RequestBody Cluster resources) {
        return new ResponseEntity<>(kafkaService.testConnectionKafka(resources), HttpStatus.CREATED);
    }
    
    @Log("kafka状态")
    @ApiOperation("kafka状态")
    @PostMapping("/status")
    @PreAuthorize("@el.check('cluster:list')")
    public ResponseEntity<Object> clusterStatus(@Validated @RequestBody Cluster resources) {
        return new ResponseEntity<>(clusterService.getClusterStatus(resources), HttpStatus.CREATED);
    }
}
